package com.hdw.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hdw.basic.model.SystemContext;
import com.hdw.cms.model.Attachment;
import com.hdw.cms.model.BaseInfo;
import com.hdw.cms.model.Channel;
import com.hdw.cms.model.ChannelType;
import com.hdw.cms.model.Topic;
import com.hdw.cms.service.AttachmentService;
import com.hdw.cms.service.ChannelService;
import com.hdw.cms.service.IndexPicService;
import com.hdw.cms.service.IndexService;
import com.hdw.cms.service.TopicService;
import com.hdw.cms.utils.FreemarkerUtil;
import com.hdw.cms.web.BaseInfoUtil;

@Service("indexService")
public class IndexServiceImpl implements IndexService {

	private String outPath;
	private FreemarkerUtil util;

	@Autowired(required = true)
	public IndexServiceImpl(String ftlPath, String outPath) {
		super();
		if (util == null) {
			this.outPath = outPath;
			util = FreemarkerUtil.getInstance(ftlPath);
		}
	}

	@Resource(name = "channelService")
	private ChannelService channelService;
	@Resource(name = "topicService")
	private TopicService topicService;

	@Resource(name = "indexPicService")
	private IndexPicService indexPicService;

	@Resource(name = "attachmentService")
	private AttachmentService attachmentService;

	@Override
	public void generateTop() {
		// System.out.println("=============重新生成了顶部信息====================");
		// List<Channel> cs = channelService.listTopNavChannel();
		List<Channel> cs = channelService.listTopNavChannel2();
		/*
		 * for (Channel channel : cs) {
		 * System.out.println(channel.getChildren().size()); }
		 */
		// List<Channel> lch = ((Channel) cs).getChildren();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("menus", cs);
		root.put("leftPic", indexPicService.getPicByOldName("float-left"));
		root.put("rightPic1", indexPicService.getPicByOldName("float-right1"));
		root.put("rightPic2", indexPicService.getPicByOldName("float-right2"));
		root.put("rightPic3", indexPicService.getPicByOldName("float-right3"));
		root.put("rightPic4", indexPicService.getPicByOldName("float-right4"));
		root.put("rightPic5", indexPicService.getPicByOldName("float-right5"));
		root.put("logo1", indexPicService.getPicByOldName("logo1"));
		root.put("logo2", indexPicService.getPicByOldName("logo2"));
		// root.put("lchs", lch);
		// 需要baseinfo处理一下这个
		root.put("baseInfo", BaseInfoUtil.getInstacne().read());
		// String outfile = SystemContext.getRealPath()+outPath+"/top.jsp";
		String outfile = SystemContext.getRealPath()+outPath+"/top.jsp";
		util.fprint(root, "/top.ftl", outfile);
	}

	// 3、首页宣传图片
	@Override
	public void generatebanner() {
		String outfile = SystemContext.getRealPath() + outPath + "/banner.jsp";
		BaseInfo bi = BaseInfoUtil.getInstacne().read();
		int picnum = bi.getIndexPicNumber();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("pics", indexPicService.listIndexPicByNum(picnum));
		util.fprint(root, "/banner.ftl", outfile);
	}

/*	// 3、首页新闻图片
	@Override
	public void generateNewpic() {
		// System.out.println("=========重新生成首页--新闻图片的信息==============");
		String outfile = "D:/test/outeam2.jsp";
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("propic", attachmentService.listIndexNewPicByNum());
		util.fprint(root, "/outeam.ftl", outfile);
	}*/

	@Override
	public void generateBody() {
		// System.out.println("=========重新生成首页--文章的的内容信息==============");
		// 1、获取所有的首页栏目
		List<Attachment> at=attachmentService.listIndexNewPicByNum();
		for(Attachment a:at)
		{
			if(a.getattName().length()>15)
			a.setattName(a.getattName().substring(0, 15));
		} 
		List<Topic> tpl = topicService.ListReTopic();
		for(Topic t:tpl){
			if(t.getTitle().length()>20)
			t.setTitle(t.getTitle().substring(0,20));
		}
		// 2、根据首页栏目创建相应的IndexTopic对象
		// 加载indexChannel.properties
		
		// 3、更新首页图片
		Map<String, Object> root = new HashMap<String, Object>();
		// root.put("newpics", attachmentService.listIndexNewPicByNum(picnum));
		root.put("adv", indexPicService.getPicByOldName("adv"));
		root.put("tpl", tpl);
		root.put("proPic", at);
		/*
		 * root.put("keywords", keywordService.getMaxTimesKeyword(12));
		 * root.put("xxgk", topicService.loadLastedTopicByColumn(7));
		 */
		String outfile = SystemContext.getRealPath()+outPath+"/body.jsp";
		util.fprint(root, "/body.ftl", outfile);
	}

	@Override
	public void generateBottom() {
		// System.out.println("=============重新生成了底部信息====================");
		List<Channel> cs = channelService.listTopNavChannel2();
		/*
		 * for (Channel channel : cs) {
		 * System.out.println(channel.getChildren().size()); }
		 */
		// List<Channel> lch = ((Channel) cs).getChildren();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("menus", cs);
		// root.put("lchs", lch);
		// 需要baseinfo处理一下这个
		root.put("baseInfo", BaseInfoUtil.getInstacne().read());
		// String outfile = SystemContext.getRealPath()+outPath+"/top.jsp";
		String outfile = SystemContext.getRealPath()+outPath+"/foot.jsp";
		util.fprint(root, "/foot.ftl", outfile);
	}


}
